// pages/login/login.js
Page({
  data: {
    account:'', //志愿者的account
    password:'',
    openid:'',
  },
  /**
   * 生命周期函数--监听页面显示
   * 1、获取家属的openid
   */
  onShow: function () { 
    this.getOpenid();
  },
  showtips:function(){
    wx.showModal({
      title:'登录提示',
      content:'志愿者系统由管理员统一注册以及修改密码！',
      showCancel: false,//是否显示取消按钮
      confirmColor: '#feb431',//确定文字的颜色
      confirmText:'知道了'
    })
  },
  /**
   * 获取账户名的输入
   */
  account_input:function(e){
    this.setData({
      account:e.detail.value
    })
    console.log("获取到登录账号为：",this.data.account);
    wx.setStorage({//存储到本地
      key:"account",
      data:this.data.account
    })
  },
  /**
   * 获取密码的输入
   */
  password_input:function(e){
    this.setData({
      password:e.detail.value
    })
    console.log("获取到的密码为：",this.data.password)
    wx.setStorage({//存储到本地
      key:"password",
      data:this.data.password
    })
  },
  /** 
   * 获取用户openid
  */
  getOpenid() {
    let that = this;
    wx.cloud.callFunction({
      name: 'getOpenid',
      complete: res => {
        console.log('云函数获取到的openid: ', res.result.openId)
        var openid = res.result.openId;
        that.setData({
          openid: openid
        })
      }
    })
  },
  /**
   * 登录
   */
  submitBtn:function(){
    wx.request({
      url: 'https://www.lanotherl.com/user/login',
      data:{
        username:this.data.account,
        password:this.data.password,
        openid:this.data.openid
      },
      method:"POST",
      // Post请求
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      success:res=>{
        console.log("接口访问成功,志愿者id：",res)
        wx.setStorage({//将taskID存储到本地
          key:"volunteerId",
          data:res.data.data.volunteerId
        })
        wx.setStorage({//将taskID存储到本地
          key:"token",
          data:res.data.data.token
        })
        wx.switchTab({
          url: '/pages/index/index',
          success: (result) => {console.log("跳转到主页成功！",result)},
          fail: (res) => {console.log("跳转到主页失败：",res)},
        })
      }
    })
  },
})